package com.vrv.im.control;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.vrv.im.domain.ComplaintBean;
import com.vrv.im.domain.ProjectBean;
import com.vrv.im.domain.UserBean;
import com.vrv.im.service.AnnexRelatedService;
import com.vrv.im.service.AnnexService;
import com.vrv.im.service.ComplaintService;
import com.vrv.im.service.UserRoleService;
import com.vrv.im.util.Constant;
import com.vrv.im.util.Util;
import com.vrv.im.vo.ComplaintVo;
import com.vrv.im.vo.ProjectVo;

@Controller
@RequestMapping("/complaint")
public class ComplaintController {

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private AnnexService annexService;
    @Autowired
    private AnnexRelatedService annexRelationService;

    @RequestMapping("/goToPage")
    public ModelAndView goToPage(@ModelAttribute("voModel") UserBean ub) {
        if (ub == null || !StringUtils.hasText(ub.getOpenId())
                || !StringUtils.hasText(ub.getToken())) {
            ModelAndView mav = new ModelAndView("userbind/bind");
            mav.addObject("voModel", new UserBean());
            mav.addObject("type", "error");
            mav.addObject("errotMsg", "请传递参数openID，tocken");
            return mav;
        }
        List<ProjectVo> list = userRoleService.getBindProjectByUserID(ub.getId());
        ProjectBean project = new ProjectBean();
        if (list == null || list.size() <= 0)
            return new ModelAndView("404");
        for (ProjectVo v : list) {
            if (v.getRoleID() == Constant.OWNER_ROLE_ID) {
                project.setProjectID(v.getProjectID());
                project.setCommunityName(v.getCommunityName());
                project.setCommunityNum(v.getCommunityNum());
                project.setContractNum(v.getContractNum());
            }
        }
        ub.setId(ub.getId());
        ub.setName(list.get(0).getUserName());
        ub.setPhone(list.get(0).getPhone());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("project", project);
        map.put("user", ub);
        map.put("openId", ub.getOpenId());
        map.put("token", ub.getToken());
        return new ModelAndView("/complaint/add", map);
    }

    @RequestMapping("/add")
    public ModelAndView addComplaint(HttpServletRequest request,
            @ModelAttribute ComplaintVo v,
            @RequestParam("file") MultipartFile[] file,
            @ModelAttribute("voModel") UserBean ub) {
        if (ub == null || !StringUtils.hasText(ub.getOpenId())
                || !StringUtils.hasText(ub.getToken())) {
            ModelAndView mav = new ModelAndView("userbind/bind");
            mav.addObject("voModel", new UserBean());
            mav.addObject("type", "error");
            mav.addObject("errotMsg", "请传递参数openID，tocken");
            return mav;
        }
        ComplaintBean b = new ComplaintBean();
        b.setUserID(ub.getId());
        b.setProjectID(v.getProjectID());
        b.setProblem(v.getProblem());
        long createTime = new Date().getTime();// (v.getCreateTime()==0?new Date().getTime():v.getCreateTime());
        b.setComplaintTime(createTime);
        long complaintID = complaintService.addComplaint(b);
        if (complaintID > 0) {
            @SuppressWarnings("deprecation")
			String realPath = request.getRealPath("/");
            List<String> fileUrls = new ArrayList<String>();
            for (MultipartFile f : file) {
                try {
                    String path = realPath + "MyFile\\";
                    String fileName = String.valueOf(new Date().getTime()) + Util.getFileType(f.getOriginalFilename());
                    File targetFile = new File(path, fileName);
                    f.transferTo(targetFile);
                    fileUrls.add("MyFile/" + fileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileUrls != null && fileUrls.size() > 0) {
                List<Long> fileIDs = annexService.addAnnex(fileUrls);
                annexRelationService.addRelation(Constant.ANNEX_TYPE_COMPLAINT, complaintID, fileIDs);
            }
        }
        Map<String, String> varMap = new HashMap<String, String>();
        varMap.put("openId", ub.getOpenId());
        varMap.put("token", ub.getToken());
        varMap.put("projectID", v.getProjectID()+"");
        varMap.put("id", ub.getId()+"");
        return new ModelAndView("redirect:/progress/goToView.do",varMap);
    }

    @RequestMapping("/viewAll")
    public ModelAndView viewAllComplaint(@ModelAttribute("voModel") UserBean ub) {
        if (ub == null || !StringUtils.hasText(ub.getOpenId())
                || !StringUtils.hasText(ub.getToken())) {
            ModelAndView mav = new ModelAndView("userbind/bind");
            mav.addObject("voModel", new UserBean());
            mav.addObject("type", "error");
            mav.addObject("errotMsg", "请传递参数openID，tocken");
            return mav;
        }
        List<ComplaintVo> list = complaintService.getAllComplaint();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("complaints", list);
        map.put("token", ub.getToken());
        map.put("openId", ub.getOpenId());
        map.put("id", ub.getId());
        return new ModelAndView("/complaint/view", map);
    }
}
